#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from PySide import QtGui, QtCore
import pymel.core as pmel
import random
from tder_maya import undoPmel


class RandomDialog(QtGui.QDialog):
    def __init__(self, parent=QtGui.QApplication.activeWindow()):
        super(RandomDialog, self).__init__(parent)
        from .pyuic.random_form import Ui_randomForm
        self._ui = Ui_randomForm()
        self._ui.setupUi(self)
        self._ui.detailWidget.hide()
        self.setStyleSheet("QLineEdit:disabled{background-color: rgb(80, 80, 80);}")
        self._ui.randomSetRunBtn.clicked.connect(self.on_random_set_btn_clicked)
        self._ui.percentSelRunBtn.clicked.connect(self.on_percent_sel_btn_clicked)
        self._ui.percentLineEdit.setText('100')
        self.set_reg("[0-9.]+", self._ui.percentLineEdit)
        self.lineEditList = [
            self._ui.offsetXMinLineEdit,
            self._ui.offsetXMaxLineEdit,
            self._ui.offsetYMinLineEdit,
            self._ui.offsetYMaxLineEdit,
            self._ui.offsetZMinLineEdit,
            self._ui.offsetZMaxLineEdit,
            self._ui.rotateXMinLineEdit,
            self._ui.rotateXMaxLineEdit,
            self._ui.rotateYMinLineEdit,
            self._ui.rotateYMaxLineEdit,
            self._ui.rotateZMinLineEdit,
            self._ui.rotateZMaxLineEdit,
            self._ui.scaleMaxLineEdit,
            self._ui.scaleMinLineEdit
        ]
        for lineEdit in self.lineEditList:
            self.set_reg("[0-9.-]+", lineEdit)

    def set_reg(self, text, lineEdit):
        self._reg = QtCore.QRegExp(text)
        self._validator = QtGui.QRegExpValidator(self._reg, lineEdit)
        lineEdit.setValidator(self._validator)

    @undoPmel
    def on_percent_sel_btn_clicked(self):
        text = self._ui.percentLineEdit.text()
        try:
            value = int(text)
        except:return
        else:
            if value > 100:
                value = 100
            if value < 0:
                value = 0
        k = value / 100.0
        if k:
            selObjList = pmel.ls(sl=1)
            count = int(len(selObjList)*k)
            objList = random.sample(selObjList, count)
            pmel.select(objList)

    @undoPmel
    def on_random_set_btn_clicked(self):
        selObjList = pmel.ls(sl=1)
        if not selObjList:
            return
        offsetXValue = self.get_min_max_value(self._ui.offsetXCheckBox,
                                              self._ui.offsetXMinLineEdit,
                                              self._ui.offsetXMaxLineEdit)

        offsetYValue = self.get_min_max_value(self._ui.offsetYCheckBox,
                                              self._ui.offsetYMinLineEdit,
                                              self._ui.offsetYMaxLineEdit)

        offsetZValue = self.get_min_max_value(self._ui.offsetZCheckBox,
                                              self._ui.offsetZMinLineEdit,
                                              self._ui.offsetZMaxLineEdit)

        rotateXValue = self.get_min_max_value(self._ui.rotateXCheckBox,
                                              self._ui.rotateXMinLineEdit,
                                              self._ui.rotateXMaxLineEdit)

        rotateYValue = self.get_min_max_value(self._ui.rotateYCheckBox,
                                              self._ui.rotateYMinLineEdit,
                                              self._ui.rotateYMaxLineEdit)

        rotateZValue = self.get_min_max_value(self._ui.rotateZCheckBox,
                                              self._ui.rotateZMinLineEdit,
                                              self._ui.rotateZMaxLineEdit)

        scaleValue = self.get_min_max_value(self._ui.scaleCheckBox,
                                            self._ui.scaleMinLineEdit,
                                            self._ui.scaleMaxLineEdit)

        for obj in selObjList:
            if offsetXValue:
                obj.tx.set(obj.tx.get() + random.uniform(offsetXValue[0], offsetXValue[1]))
            if offsetYValue:
                obj.ty.set(obj.ty.get() + random.uniform(offsetYValue[0], offsetYValue[1]))
            if offsetZValue:
                obj.tz.set(obj.tz.get() + random.uniform(offsetZValue[0], offsetZValue[1]))

            if rotateXValue:
                obj.rx.set(random.uniform(rotateXValue[0], rotateXValue[1]))
            if rotateYValue:
                obj.ry.set(random.uniform(rotateYValue[0], rotateYValue[1]))
            if rotateZValue:
                obj.rz.set(random.uniform(rotateZValue[0], rotateZValue[1]))

            if scaleValue:
                value = random.uniform(scaleValue[0], scaleValue[1])
                obj.scale.set([value, value, value])





    def get_min_max_value(self, checkBox, minLineEdit, maxLineEdit):
        if not checkBox.isChecked():
            return
        minVale = float(minLineEdit.text())
        maxVale = float(maxLineEdit.text())
        if minVale > maxVale:
            return
        return (minVale, maxVale)
